//
//  DwtFoodDetailController.m
//  生活小助手
//
//  Created by dwt on 14-8-9.
//  Copyright (c) 2014年 lxz. All rights reserved.
//

#import "DwtFoodDetailController.h"
#import "DwtFootCookData.h"
#import "DwtFootCookStep.h"
#import "UIImageView+WebCache.h"
@interface DwtFoodDetailController ()
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
/** 菜的title */
@property (weak,nonatomic) UILabel *titleFoot;
/** 菜的简介 */
@property (weak,nonatomic) UILabel *imtro;
/** 所需配料 */
@property (weak,nonatomic) UILabel *ingredients;
/** 所需调料 */
@property (weak,nonatomic) UILabel *burden;
/** 简介图片 */
@property (weak,nonatomic) UIImageView *albums;

@property (nonatomic,strong)NSMutableArray *stepImg;
@property (nonatomic,strong)NSMutableArray *stepLable;
@end

@implementation DwtFoodDetailController
-(NSMutableArray *)stepImg{
    if (_stepImg==nil) {
        _stepImg = [NSMutableArray array];
    }
    return _stepImg;
}

-(NSMutableArray *)stepLable{
if (_stepLable==nil) {
        _stepLable = [NSMutableArray array];
    }
    return _stepLable;
}



-(instancetype)initWithFootCookData:(DwtFootCookData *)footCookData{
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    


    
    /*****添加控件********/
    [self.scrollView setBackgroundColor:[UIColor whiteColor]];
    
    UILabel *titleFoot=[[UILabel alloc]init];
    self.titleFoot=titleFoot;
    [self.titleFoot setFont:[UIFont systemFontOfSize:17]];
    [self.scrollView addSubview:titleFoot];
    
    UILabel *imtro=[[UILabel alloc]init];
    self.imtro=imtro;
    imtro.numberOfLines=0;
    [self.imtro setFont:[UIFont systemFontOfSize:14]];
    [self.scrollView addSubview:imtro];
    
    UILabel *ingredients=[[UILabel alloc]init];
    self.ingredients=ingredients;
    ingredients.numberOfLines=0;
    [self.ingredients setFont:[UIFont systemFontOfSize:14]];
    [self.scrollView addSubview:ingredients];
    
    UILabel *burden=[[UILabel alloc]init];
    self.burden=burden;
    burden.numberOfLines=0;
    [self.burden setFont:[UIFont systemFontOfSize:14]];
    [self.scrollView addSubview:burden];
    
    UIImageView *albums=[[UIImageView alloc]init];
    self.albums=albums;
    [self.scrollView addSubview:albums];
    
    
    
    self.footCookData=footCookData;
    
    return  self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];

}
-(void)setFootCookData:(DwtFootCookData *)footCookData{
    _footCookData = footCookData;

    /** 设置头部固定信息 **/
    self.titleFoot.text=_footCookData.title;
    self.imtro.text= [NSString stringWithFormat:@"简介:%@",footCookData.imtro];
    self.ingredients.text= [NSString stringWithFormat:@"配料:%@",footCookData.ingredients];
    self.burden.text= [NSString stringWithFormat:@"辅料:%@",footCookData.burden];
    NSURL *urlImg=[NSURL URLWithString:[footCookData.albums lastObject]];
    [_albums setImageWithURL:urlImg placeholderImage:[UIImage imageNamed:@"statusdetail_icon_empty_failed"]];;
    
  /** 根据步骤数添加lable和img **/
    NSArray *steps=footCookData.steps;
    for (DwtFootCookStep *step in steps) {
        UIImageView *imgView = [[UIImageView alloc]init];
        NSURL *urlImg=[NSURL URLWithString:step.img];
        [imgView setImageWithURL:urlImg placeholderImage:[UIImage imageNamed:@"statusdetail_icon_empty_failed"]];
        
        [self.scrollView addSubview:imgView];
        [self.stepImg addObject:imgView];
        
        
        UILabel *lable = [[UILabel alloc]init];
        [lable setFont:[UIFont systemFontOfSize:14]];
        lable.numberOfLines=0;
        lable.text=step.step;
        
        [self.scrollView addSubview:lable];
        [self.stepLable addObject:lable];
    }
}

-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    
/*******菜谱标题设置***********/
    CGSize titleFootSize =[self getTextSize:self.titleFoot];

    CGFloat paaing = 10;
    CGFloat titleFootX=paaing;
    CGFloat titleFootY=0+paaing;
    CGFloat titleFootWidth =[UIScreen mainScreen].bounds.size.width-paaing;
    CGFloat titleFootHeight =titleFootSize.height;
    self.titleFoot.frame = CGRectMake(titleFootX, titleFootY, titleFootWidth, titleFootHeight);
    self.titleFoot.textAlignment = NSTextAlignmentCenter;
    
/*******菜谱简介设置***********/
    
    CGSize  imtroSize = [self getTextSize:self.imtro];
    
    CGFloat imtroSizeX=paaing;
    CGFloat imtroSizeY=titleFootY+titleFootHeight;
    CGFloat imtroSizeWidth =[UIScreen mainScreen].bounds.size.width-paaing;
    CGFloat imtroSizeHeight =imtroSize.height;
    self.imtro.frame =  CGRectMake(imtroSizeX, imtroSizeY, imtroSizeWidth, imtroSizeHeight);

/********菜谱简介图片设置****************/
    
    CGFloat albumsSizeWidth =190;
    CGFloat albumsSizeHeight =135;
    CGFloat albumsX=([UIScreen mainScreen].bounds.size.width-albumsSizeWidth)/2;
    CGFloat albumsY=imtroSizeY+imtroSizeHeight+paaing;

    self.albums.frame =  CGRectMake(albumsX, albumsY, albumsSizeWidth, albumsSizeHeight);
    
/********菜谱主料设置****************/
    CGSize  ingredients = [self getTextSize:self.ingredients];
    
    CGFloat ingredientsSizeX=paaing;
    CGFloat ingredientsSizeY=albumsY+albumsSizeHeight+paaing;
    CGFloat ingredientsSizeWidth =[UIScreen mainScreen].bounds.size.width-paaing;
    CGFloat ingredientsSizeHeight =ingredients.height;
    self.ingredients.frame =  CGRectMake(ingredientsSizeX, ingredientsSizeY, ingredientsSizeWidth, ingredientsSizeHeight);
    
/********菜谱辅料设置****************/
    CGSize  burden = [self getTextSize:self.burden];

    CGFloat burdenSizeX=paaing;
    CGFloat burdenSizeY=ingredientsSizeY+ingredientsSizeHeight+paaing;
    CGFloat burdenSizeWidth =[UIScreen mainScreen].bounds.size.width-paaing;
    CGFloat burdenSizeHeight =burden.height;
    self.burden.frame =  CGRectMake(burdenSizeX, burdenSizeY, burdenSizeWidth, burdenSizeHeight);
    

    CGFloat stepImgWidth = 190;
    CGFloat stepImgHeight = 135;
    CGFloat stepImgX=([UIScreen mainScreen].bounds.size.width-stepImgWidth)/2;;
    CGFloat stepImgY=burdenSizeY+burdenSizeHeight+paaing;
    
    CGFloat stepLableWidth = [UIScreen mainScreen].bounds.size.width-paaing;
    CGFloat stepLableHeight = 0;
    CGFloat stepLableX=paaing;
    CGFloat stepLableY=stepImgY+stepImgHeight;
    
    for (int i=0 ; i<self.stepImg.count; i++) {
        UIImageView *stepImg = self.stepImg[i];
        UILabel *stepLable = self.stepLable[i];
        
        stepImg.frame = CGRectMake(stepImgX, stepImgY, stepImgWidth, stepImgHeight);
        
        stepLableHeight = [self getTextSize:stepLable].height;
        
        stepLable.frame = CGRectMake(stepLableX, stepLableY, stepLableWidth, stepLableHeight);
        stepImgY= stepImgY+stepImgHeight+stepLableHeight+paaing;// burdenSizeY+burdenSizeHeight+paaing;

        stepLableY=stepImgY+stepImgHeight+paaing;
        
    }

    self.scrollView.contentSize=CGSizeMake(320, stepLableY);

}




-(CGSize)getTextSize:(UILabel *)textLabe{
    CGSize maxSize = CGSizeMake(300, MAXFLOAT);
    
    NSDictionary *dict=@{NSFontAttributeName: textLabe.font};
    CGSize size=[textLabe.text boundingRectWithSize:maxSize  options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;

     NSLog(@"textLable.text=%@, SIZE=%@",textLabe.text,NSStringFromCGSize(size));
   return [textLabe.text boundingRectWithSize:maxSize  options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
}
@end
